package me.hys.service.system.impl;

import java.util.List;

import me.hys.dao.system.IModuleMapper;
import me.hys.entity.system.ModuleEntity;
import me.hys.service.system.IModuleManager;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

/**
 * Author: @author HYS . <br>
 * Date: 2015-9-12 下午4:00:22 . <br>
 * Function: 菜单ServiceImpl
 */
@Service
@Transactional
public class ModuleManagerImpl implements IModuleManager {
	@Autowired
	private IModuleMapper mapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.system.IModuleManager#findAll()
	 */
	@Override
	public List<ModuleEntity> findAll() {
		List<ModuleEntity> modules = mapper.findAll();
		List<ModuleEntity> list = Lists.newArrayList();
		for (ModuleEntity module : modules) {
			if (module.getParent() == 0) {
				module.setChildren(getChildren(module.getId(), modules));
				list.add(module);
			}
		}
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see me.hys.service.system.IModuleManager#findCurrent(java.lang.String)
	 */
	@Override
	public List<ModuleEntity> findCurrent(String flag) {
		return mapper.findCurrent(flag);
	}

	private List<ModuleEntity> getChildren(int id, List<ModuleEntity> modules) {
		List<ModuleEntity> list = Lists.newArrayList();
		for (ModuleEntity module : modules) {
			if (module.getParent() == id) {
				list.add(module);
			}
		}
		return list;
	}
}
